<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button @click="showAddCateDialog" type="primary"
            >添加分类</el-button
          >
        </el-col>
      </el-row>

      <!-- 表格 -->
      <tree-table
        class="treeTable"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <!-- 是否有效 -->
        <template v-slot:isok="scope">
          <i
            style="color: lightgreen"
            v-if="scope.row.cat_deleted === false"
            class="el-icon-success"
          ></i>
          <i style="color: red" v-else class="el-icon-error"></i>
        </template>
        <!-- 排序 -->
        <template v-slot:order="scope">
          <el-tag v-if="scope.row.cat_level === 0" size="small">一级</el-tag>
          <el-tag
            v-else-if="scope.row.cat_level === 1"
            size="small"
            type="success"
            >二级</el-tag
          >
          <el-tag v-else size="small" type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template v-slot:opt="">
          <el-button size="small" type="primary" icon="el-icon-edit">
            编辑
          </el-button>
          <el-button size="small" type="danger" icon="el-icon-delete">
            删除
          </el-button>
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close='addCateDialogClosed'
    >
      <!-- 分类表单 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>

        <el-form-item label="父级分类">
          <!-- options 指定数据源 -->
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            :clearable="true"
            :change-on-select="true"
            @change="parentCateChange"
          ></el-cascader>
          
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getCateListAjax, getParentCateListAjax,addCateFormAjax } from "@/utils/api";
export default {
  data() {
    return {
      // 商品分类的数据
      cateList: [],
      // 查询条件
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 总数据条数
      total: 0,
      // 列的定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          labe1: "是否有效",
          type: "template",
          template: "isok",
        },
        {
          labe1: "排序",
          type: "template",
          template: "order",
        },
        {
          labe1: "操作",
          type: "template",
          template: "opt",
        },
      ],
      // 控制添加分类对话框显示隐藏
      addCateDialogVisible: false,
      // 添加分类的表单数据
      addCateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      // 分类校验规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      // 父级分类的列表
      parentCateList: [],
      // 指定级联选择器的对象
      cascaderProps:{
        expandTrigger: 'hover',
        checkStrictly:'true',
        value: 'cat_id',
        label: 'cat_name',
        children:'children'
      },
      // 选中的父级id的数据
      selectedKeys:[]
    };
  },
  methods: {
    // 获取数据
    async getCateList() {
      const { data: res } = await getCateListAjax(this.querInfo);
      console.log(res);
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    // 监听pagesize
    handleSizeChange(newSize) {
      this.querInfo.pagesize = newSize;
      this.getCateList();
    },
    // 监听pagenum
    handleCurrentChange(newPage) {
      this.querInfo.pagenum = newPage;
      this.getCateList();
    },
    //  点击按钮展示分类对话框
    showAddCateDialog() {
      this.getParentCateList();
      this.addCateDialogVisible = true;
    },
    // 获取父级分类的数据列表
    async getParentCateList() {
      const { data: res } = await getParentCateListAjax();
      this.parentCateList = res.data;
    },
    // 选择项发生变化  触发这个函数
    parentCateChange(){
      console.log(this.selectedKeys);
    //   如果selectedKeys 数据中的length 大于0 证明选中父级分类
      if(this.selectedKeys.length > 0){
        // 父级分类的id
        this.addCateForm.cat_pid =  this.selectedKeys[this.selectedKeys.length -1]
        // 为当前 分类等级赋值
        this.addCateForm.cat_level = this.selectedKeys.length;
        return
      }else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level= 0
      }
    },
    // 点击确认添加分类
    addCate(){
      console.log(this.addCateForm);
      this.$refs.addCateFormRef.validate(async valid => {
        if(!valid) return 
        const {data:res} = await addCateFormAjax(this.addCateForm);
        this.$message.success(res.meta.msg)
        this.getCateList();
        this.addCateDialogVisible = false
      })
    },
    // 监听对话框 重置表单数据
    addCateDialogClosed(){
      this.$refs.addCateFormRef.resetFields();
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    }
  },
  created() {
    this.getCateList();
  },
};
</script>
<style lang='less' scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>